package com.config.exception;

import com.alibaba.fastjson.JSONObject;
import com.config.exception.MyException;
import com.controllers.forms.SimpleResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2017/5/15.
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(MyException.class)
    //    @ExceptionHandler(value={RuntimeException.class,MyRuntimeException.class})
    //    @ExceptionHandler//处理所有异常
    @ResponseBody //在返回自定义相应类的情况下必须有，这是@ControllerAdvice注解的规定
    public JSONObject jsonErrorHandler(MyException e, HttpServletResponse response) {
        return SimpleResponse.fail(e.getMessage());
    }

}
